﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace YArchitech.Temp
{
	public class HYPipeSystem : HYSystem
	{
		public HYPipeSystem.PipeSystemType SystemType { get; set; }

		public HYPipeSystem()
		{
			base.Name = "鸿业水系统";
			this.SystemType = HYPipeSystem.PipeSystemType.Supply;
		}

		public override void WriteXml(XElement xe)
		{
			base.WriteXml(xe);
			xe.SetAttributeValue("SystemType", (int)this.SystemType);
		}

		public override void ReadXml(XElement xe)
		{
			base.ReadXml(xe);
			this.SystemType = (HYPipeSystem.PipeSystemType)xe.GetIntAttribute("SystemType", 0);
		}

		private bool DesignByVelocity(HYPipeSystemOption option, HYPipeSpecifications Specs, FlowToSpecifications flowToSpecs)
		{
			this.m_RootObject.ReAllocateFlow();
			foreach (HYMEPObject hymepobject in from pl in this.m_Objects
			where pl is HYPipeStraight
			orderby pl.Flow
			select pl)
			{
				HYPipeStraight hypipeStraight = (HYPipeStraight)hymepobject;
				FlowToSpecification specification = flowToSpecs.GetSpecification(hypipeStraight.Flow);
				if (specification != null)
				{
					hypipeStraight.Section = specification.Specification;
					hypipeStraight.Status = specification.Status;
				}
				else
				{
					HYPipeSpecification hypipeSpecification = new HYPipeSpecification();
					bool flag = Specs.SelectSpecificationByVelocity(hypipeStraight.Flow, out hypipeSpecification);
					hypipeStraight.Section = hypipeSpecification;
					hypipeStraight.Status = (flag ? PipeSizeStatus.Suitable : PipeSizeStatus.Unsuitable);
					flowToSpecs.Add(new FlowToSpecification
					{
						Flow = hypipeStraight.Flow,
						Specification = hypipeSpecification,
						Status = hypipeStraight.Status
					});
				}
			}
			return true;
		}

		private bool DesignByLoad(HYPipeSystemOption option, HYPipeSpecifications Specs, FlowToSpecifications flowToSpecs)
		{
			this.m_RootObject.ReAllocateFlow();
			foreach (HYMEPObject hymepobject in from pl in this.m_Objects
			where pl is HYPipeStraight
			orderby (pl as HYPipeStraight).Load
			select pl)
			{
				HYPipeStraight hypipeStraight = (HYPipeStraight)hymepobject;
				FlowToSpecification specification = flowToSpecs.GetSpecification(hypipeStraight.Load);
				if (specification != null)
				{
					hypipeStraight.Section = specification.Specification;
					hypipeStraight.Status = specification.Status;
				}
				else
				{
					HYPipeSpecification hypipeSpecification = new HYPipeSpecification();
					bool flag = Specs.SelectSpecificationByLoad(hypipeStraight.Load, out hypipeSpecification);
					hypipeStraight.Section = hypipeSpecification;
					hypipeStraight.Status = (flag ? PipeSizeStatus.Suitable : PipeSizeStatus.Unsuitable);
					flowToSpecs.Add(new FlowToSpecification
					{
						Flow = hypipeStraight.Load,
						Specification = hypipeSpecification,
						Status = hypipeStraight.Status
					});
				}
			}
			return true;
		}

		private bool DesignByRm(HYPipeSystemOption option, HYPipeSpecifications Specs, FlowToSpecifications flowToSpecs)
		{
			foreach (HYMEPObject hymepobject in from pl in this.m_Objects
			where pl is HYPipeStraight
			orderby pl.Flow
			select pl)
			{
				HYPipeStraight hypipeStraight = (HYPipeStraight)hymepobject;
				FlowToSpecification specification = flowToSpecs.GetSpecification(hypipeStraight.Flow);
				if (specification != null)
				{
					hypipeStraight.Section = specification.Specification;
					hypipeStraight.Status = specification.Status;
				}
				else
				{
					double num = double.MaxValue;
					HYPipeSpecification section = null;
					bool flag = false;
					foreach (HYPipeSpecification hypipeSpecification in Specs)
					{
						hypipeStraight.Section = hypipeSpecification;
						hypipeStraight.CalVelocity();
						double value = hypipeStraight.CalRm(option);
						if (hypipeSpecification.RmRange.Contains(value))
						{
							hypipeStraight.Status = PipeSizeStatus.Suitable;
							flag = true;
							break;
						}
						double minDistance = hypipeSpecification.RmRange.GetMinDistance(value);
						if (num > minDistance)
						{
							num = minDistance;
							section = hypipeSpecification;
						}
					}
					if (!flag)
					{
						hypipeStraight.Section = section;
						hypipeStraight.Status = PipeSizeStatus.Unsuitable;
					}
					flowToSpecs.Add(new FlowToSpecification
					{
						Flow = hypipeStraight.Flow,
						Specification = hypipeStraight.Section,
						Status = hypipeStraight.Status
					});
				}
			}
			return true;
		}

		public bool Design(HYPipeSystemOption option, HYPipeSpecifications Specs)
		{
			FlowToSpecifications flowToSpecs = new FlowToSpecifications();
			switch (option.Method)
			{
			case HYPipeSystemOption.CalMethod.ByVelocity:
				this.DesignByVelocity(option, Specs, flowToSpecs);
				break;
			case HYPipeSystemOption.CalMethod.ByRm:
				this.DesignByRm(option, Specs, flowToSpecs);
				break;
			case HYPipeSystemOption.CalMethod.ByLoad:
				this.DesignByLoad(option, Specs, flowToSpecs);
				break;
			}
			return this.Check(option);
		}

		public void CalAttachedObjects(HYPipeSystemOption option)
		{
			foreach (HYMEPObject hymepobject in from att in this.m_Objects
			where !(att is HYPipeline)
			select att)
			{
				hymepobject.Cal(option);
			}
		}

		public bool Check(HYPipeSystemOption option)
		{
			List<HYPipeline> list;
			base.GetAllPipelines(out list);
			foreach (HYPipeline hypipeline in list)
			{
				hypipeline.CalVelocity();
			}
			this.CalAttachedObjects(option);
			foreach (HYPipeline hypipeline2 in list)
			{
				(hypipeline2 as HYPipeStraight).Cal(option);
			}
			foreach (HYBranch hybranch in this.m_Branches)
			{
				hybranch.Cal();
			}
			HYBranch maxResistanceBranch = base.GetMaxResistanceBranch();
			foreach (HYBranch hybranch2 in this.m_Branches)
			{
				hybranch2.CalUnbalanceRate(maxResistanceBranch.Resistance);
			}
			return true;
		}

		public enum PipeSystemType
		{
			Supply,
			Return,
			Condensation
		}
	}
}
